//
//  NotificationNavView.m
//  i5db
//
//  Created by iMuse on 15-5-21.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "NotificationNavView.h"
#define viewWeight 300
#define viewHeight 30
#define OPACITY    0.6
#define DURATION   0.25f
#define STOP_TIME  2.0f

@implementation NotificationNavView
{
    UIView      * navView;
    UILabel     * label;
    UIImageView * imageView;
    
}

- (id)initWithFrame:(CGRect)frame text:(NSString*)text
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        navView = [[UIView alloc] init];
        navView.frame = CGRectMake((SCREEN_WIDTH-viewWeight)/2, 64, viewWeight, 0);
        navView.backgroundColor = [UIColor blackColor];
       navView.layer.opacity = OPACITY;
       
        [self addSubview:navView];
        imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(100, (viewHeight-16)/2, 16, 16);
        [navView addSubview:imageView];
        
        label = [[UILabel alloc] init];
        label.text = text;
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = [UIColor whiteColor];
        label.frame = CGRectMake(imageView.frame.size.width+imageView.frame.origin.x+10, imageView.frame.origin.y, 200, 16);
        [navView addSubview:label];
        
       
        
        
    }
    return self;
}

-(void)setType:(NotificationType)type
{
    if (type == NotificationTypeWarrning)
    {
        imageView.image = [UIImage imageNamed:@"notification_warring"];
        
    }else if (type == NotificationTypeSuccess)
    {
        imageView.image = [UIImage imageNamed:@"notification_success"];
    }
}
-(void)showAnimation
{
    [UIView animateWithDuration:DURATION animations:^{
        
        navView.frame = CGRectMake(navView.frame.origin.x, navView.frame.origin.y, viewWeight, viewHeight);
    }completion:^(BOOL finash){
        
        [self performSelector:@selector(removeAnimation) withObject:nil afterDelay:STOP_TIME];
    }];
}
-(void) removeAnimation
{
    [UIView animateWithDuration:DURATION animations:^{
        
        navView.frame = CGRectMake(navView.frame.origin.x, navView.frame.origin.y, viewWeight, 0);
    }completion:^(BOOL finash){
        
        [self removeFromSuperview];
    }];
}

+(UIViewController*)rootController
{
    UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    while (topController.presentedViewController) {
        topController = topController.presentedViewController;
    }
    return topController;
}

+(void) showMessage:(NSString*) text
{
    [self showMessage:text type:NotificationTypeWarrning];
}
+(void) showMessage: (NSString*) text type:(NotificationType) type
{
    UIViewController * rootcontroller =  [self rootController];
    
    NotificationNavView * notiView = [[NotificationNavView alloc] initWithFrame:rootcontroller.view.bounds text:text];
    
    [rootcontroller.view addSubview:notiView];
    notiView.type = type;
    
    [notiView showAnimation];
}

@end
